﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Supply_Fetch
{
    public partial class DrawerTable : UserControl
    {
        private List<Button> drawers;
        private Button selectedDrawer;
        private Button secondSelectedDrawer;
        private int MaxCols;
        private int MaxRows;
        private bool isSecondSelectable;

        public event EventHandler drawerButton_Click;

        public DrawerTable()
        {
            InitializeComponent();
        }

        /// <summary>
        /// This method sets up the table.
        /// </summary>
        /// <param name="maxRows"></param>
        /// <param name="maxCols"></param>
        public void buildTable(int maxRows, int maxCols)
        {
            drawers = new List<Button>();
            int height = this.Height / maxRows;
            int width = this.Width / maxCols;
            this.MaxCols = maxCols;
            this.MaxRows = maxRows;
            this.selectedDrawer = null;
            this.secondSelectedDrawer = null;
            this.isSecondSelectable = false;

            int count = 0;

            for (int i = 0; i < maxRows; i++)
            {
                for (int j = 0; j < maxCols; j++)
                {
                    drawers.Add(new Button());
                    drawers[count].TextAlign = ContentAlignment.MiddleCenter;
                    drawers[count].FlatStyle = FlatStyle.Flat;
                    drawers[count].Height = height;
                    drawers[count].Width = width;
                    drawers[count].Name = (i+1).ToString() + "," + (j+1).ToString();
                    drawers[count].Text = (count + 1).ToString();
                    drawers[count].Location = new Point(j * width, i * height);
                    drawers[count].Font = new Font("Microsoft Sans Serif", 15.75F, FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    drawers[count].Click += new System.EventHandler(this.allDrawers_onClick);
                    this.Controls.Add(drawers[count]);

                    count++;
                }
            }

            this.ColorDrawers();
        }

        /// <summary>
        /// This method notifies if a drawer is selected.
        /// Note: This requires an event handler in the using class to handle this event.
        /// Note: Also this method will select the second drawer if first drawer is not null, so proper use of deselectDrawers method is necessary by caller.
        /// </summary>
        /// <param name="sender">The control that sent the event.</param>
        /// <param name="e">General information about the event.</param>
        private void allDrawers_onClick(object sender, EventArgs e)
        {
            if (this.drawerButton_Click != null)
            {
                this.ColorDrawers();

                if (this.isSecondSelectable == false)
                {
                    this.DeSelectDrawers();
                }
           
                if (this.isSecondSelectable == true)
                {
                    this.secondSelectedDrawer = (Button)sender;
                }
                else
                {
                    this.selectedDrawer = (Button)sender;
                }

                this.HighlightSelectedDrawers();
                this.drawerButton_Click(sender, e);
            }
        }

        /// <summary>
        /// This method highlights drawers in the drawer table requested by the caller.
        /// </summary>
        /// <param name="locations">This contains a list of drawer locations to be highlighted in the Drawer Table gui map.</param>
        public void HighlightDrawers(string[] locations)
        {
            for (int i = 0; i < this.drawers.Count(); i++)
            {
                for (int j = 0; j < locations.Length; j++)
                {
                    if (drawers[i].Name == locations[j])
                    {
                        drawers[i].BackColor = ControlPaint.Light(Color.Yellow);
                        break;
                    }
                }
            }

            this.HighlightSelectedDrawers();
        }

        /// <summary>
        /// This sets the colors of the drawers back to their default colors.
        /// </summary>
        public void ColorDrawers()
        {
            Color paintBrush;
            int index = 0;

            for (int i = 0; i < this.MaxRows; i++)
            {
                if (i == 0)
                {
                    paintBrush = Color.Salmon;
                }
                else if (i == 1)
                {
                    paintBrush = Color.LightBlue;
                }
                else if (i == 2)
                {
                    paintBrush = Color.LightGreen;
                }
                else if (i == 3)
                {
                    paintBrush = Color.PaleVioletRed;
                }
                else
                {
                    paintBrush = Color.Gray;
                }

                for (int j = 0; j < this.MaxCols; j++)
                {
                    this.drawers[index].BackColor = paintBrush;
                    index++;
                }
            }
        }

        /// <summary>
        /// This highlights the drawer selected by the user.
        /// Note: also highlights a second drawer if necessary for certain operations.
        /// </summary>
        public void HighlightSelectedDrawers()
        {
            if (this.selectedDrawer != null)
            {
                this.selectedDrawer.BackColor = Color.Yellow;
                this.selectedDrawer.ForeColor = Color.Red;

                if (this.secondSelectedDrawer != null)
                {
                    this.secondSelectedDrawer.BackColor = Color.Orange;
                    this.secondSelectedDrawer.ForeColor = Color.Red;
                }
            }
        }

        /// <summary>
        /// This sets the selected drawer value to null, to notify that it is no longer selected.
        /// Note: Also deselects second drawer if it is selected.
        /// </summary>
        public void DeSelectDrawers()
        {
            if (this.selectedDrawer != null)
            {
                this.selectedDrawer.ForeColor = Color.Black;

                if (this.secondSelectedDrawer != null)
                {
                    this.secondSelectedDrawer.ForeColor = Color.Black;
                }
            }

            this.selectedDrawer = null;
            this.secondSelectedDrawer = null;
            this.isSecondSelectable = false;
        }

        /// <summary>
        /// This returns the drawer selected by the user.
        /// </summary>
        public Button SelectedDrawer
        {
            get { return this.selectedDrawer; }
        }

        /// <summary>
        /// Changes whether the first drawer is selectable or not on call of this method.
        /// </summary>
        public void changeSelectionMode()
        {
            if (this.isSecondSelectable == true)
            {
                this.isSecondSelectable = false;
                this.secondSelectedDrawer.ForeColor = Color.Black;
                this.secondSelectedDrawer = null;
            }
            else
            {
                this.isSecondSelectable = true;
            }
        }

        /// <summary>
        /// This returns the second drawer selected by the user.
        /// </summary>
        public Button SecondSelectedDrawer
        {
            get { return this.secondSelectedDrawer; }
        }
    }
}
